<template>

  <div class="search">
        <el-form :inline="true">
      <el-row :gutter="10">
        <template v-for="(item, i) in searchDataReal">
            <el-form-item v-if="item.type === 'input'" :key="i" :label="$t(item.label)">
						  <el-input  clearable size="small" :style="{width:'130px'}" v-model="item.value" :maxlength='item.maxLength'/>
            </el-form-item>
            <el-form-item v-if="item.type === 'select'" :key="i" :label="$t(item.label)">  
              <el-select clearable v-if="item.langFlag" v-model="item.value" size="small">
                <el-option  v-for="(op, index) in item.option" :key="index" :label="$t(op.label)" :value="op.value"></el-option>
              </el-select>
              <el-select clearable v-model="item.value" size="small" v-else>
                <el-option  v-for="(op, index) in item.option" :key="index" :label="op.label" :value="op.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.type === 'filter'" :key="i" :label="$t(item.label)">  
              <el-select clearable filterable v-if="item.langFlag" v-model="item.value" size="small">
                <el-option  v-for="(op, index) in item.option" :key="index" :label="$t(op.label)" :value="op.value"></el-option>
              </el-select>
              <el-select clearable filterable v-model="item.value" size="small" v-else>
                <el-option  v-for="(op, index) in item.option" :key="index" :label="op.label" :value="op.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.type === 'rangePicker'" :key="i" :label="$t(item.label)">
              <el-date-picker  
							size="small"
							v-model="item.value" 
							value-format="yyyy-MM-dd" 
							type="daterange" 
							range-separator="~" 
							:default-value="$store.state.common.preMonth" 
							:start-placeholder="$t('public.beginTime')" 
							:end-placeholder="$t('public.endTime')"/>  
            </el-form-item>
            <el-form-item v-if="item.type === 'date'" :key="i" :label="$t(item.label)">
              <el-date-picker  
							size="small"
							v-model="item.value" 
							value-format="yyyy-MM-dd" 
							type="date" />  
            </el-form-item>
            <el-form-item v-if="item.type === 'doubleInput'" :key="i" :label="$t(item.label)">
              <el-input style="width:80px;" v-model="item.value[0]" maxlength='4' size="small" clearable/>
						  ~
						  <el-input style="width:80px;" v-model="item.value[1]" maxlength='4' size="small" clearable/>
            </el-form-item>
        </template>  
        <el-form-item v-if="searchRight">
          <el-button type="primary" size="small" @click="search"><span>{{$t('public.select')}}</span></el-button>
          <el-button @click="resetForm()" size="small">{{$t('public.reset')}}</el-button>
        </el-form-item>
        <el-form-item v-if="outputRight">
          <el-button type="primary" size="small" @click="putExcel"><span>{{$t('public.excel')}}</span></el-button>   
        </el-form-item>  
      </el-row>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
			searchDataReal: [],
			queryFlag: false,
			excelFlag: false,
      phoneValue: '',
    };
  },
  props: {
    searchData: {
      type: Array,
      default: [],
    },
		searchRight: {
      type: Boolean,
      default: false
    },
    outputRight: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    // filter(val, oldVal) {
    //   this.$emit('change', val, oldVal);
    // },
    searchData(val) {
      this.$nextTick(() => {
        console.log(2);
        this.searchDataReal = val;
      });
    },
  },
  mounted() {
    this.$nextTick(() => {
      console.log(1);
      this.searchDataReal = this.searchData;
    });
  },
  computed: {
    filter() {
      const obj = {};
      console.log(this.searchDataReal);
      this.searchDataReal.forEach((item) => {
        if(item.type=='doubleInput'){
          obj[item.attr[0]] = item.value[0];
          obj[item.attr[1]] = item.value[1];
        }else{
          obj[item.attr] = item.value;
        }    
      });
      return obj;
    },
  },
  methods: {
    search() {
      this.$emit('search', this.filter);
    },
    putExcel() {
      this.$emit('putExcel', this.filter);
    },
    resetForm() {
       this.searchDataReal.forEach((item) => {
         if(item.type=='select'){
           item.value="";
         }else if(item.type=='rangePicker'){
           item.value=[];
         }else if(item.type=='doubleInput'){
           item.value=['',''];
         }else{
           item.value="";
         }
         
      });
    }
  },
};
</script>
